package utils

import (
	"bufio"
	"os"
	"sync"
)

var (
	sensitiveWords []string
	once           sync.Once
)

// LoadSensitiveWords 从指定文件加载敏感词列表
func LoadSensitiveWords(filePath string) error {
	var err error
	once.Do(func() {
		err = loadWordsFromFile(filePath)
	})
	return err
}

func loadWordsFromFile(filePath string) error {
	file, err := os.Open(filePath)
	if err != nil {
		return err
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		sensitiveWords = append(sensitiveWords, scanner.Text())
	}

	return scanner.Err()
}

// GetSensitiveWords 返回
func GetSensitiveWords() []string {
	var wordsCopy []string
	wordsCopy = append(wordsCopy, sensitiveWords...)
	return wordsCopy
}
